package com.example.shiwu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.shiwu.AlarmListActivity;
import com.example.shiwu.ArrangeAddActivity;
import com.example.shiwu.DeviceListActivity;
import com.example.shiwu.GatherDataActivity;
//import com.example.shiwu.HeadActivity;
import com.example.shiwu.GpsListActivity;
import com.example.shiwu.ItemInfoActivity;
import com.example.shiwu.ItemInfoListActivity;
import com.example.shiwu.R;
import com.example.shiwu.SendReceiveAddActivity;
import com.example.shiwu.SendReceiveAddQueueActivity;
import com.example.shiwu.SendReceiveAddTcpServiceActivity;
import com.example.shiwu.SendReceiveListActivity;
import com.example.shiwu.db.model.Arrange;
import com.example.shiwu.db.model.Device;
import com.example.shiwu.db.model.Items;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.service.ArrangeService;
import com.example.shiwu.service.DeviceService;
import com.example.shiwu.service.ItemsService;
import com.example.shiwu.service.SendsService;

import org.jetbrains.annotations.Nullable;

import java.util.List;

public class MainRoutineFragment extends Fragment implements View.OnClickListener {
    private ItemsService itemsService;
    private DeviceService deviceService;
    private ArrangeService arrangeService;
    private SendsService sendsService;
    private  View view;
    private int itmeslistLen,arrangeLen,sendsLen;
    private Items itmesMain;
    private Sends sends;

    private Integer deviceSta=0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_mainroutine,container,false);
        TextView tx = view.findViewById(R.id.item_info_add);
        tx.setOnClickListener(this);
        TextView tx1 = view.findViewById(R.id.item_info);
        tx1.setOnClickListener(this);

        TextView tx2 = view.findViewById(R.id.tba_menu_devicedata);
        tx2.setOnClickListener(this);
        TextView tx3 = view.findViewById(R.id.tba_menu_inoutlist);
        tx3.setOnClickListener(this);
        TextView tx4 = view.findViewById(R.id.tba_menu_inout);
        tx4.setOnClickListener(this);
        TextView tx5 = view.findViewById(R.id.tba_menu_permutation);
        tx5.setOnClickListener(this);
        TextView tx6 = view.findViewById(R.id.tba_menu_datamining);
        tx6.setOnClickListener(this);
        TextView tx7 = view.findViewById(R.id.alarm_btn);
        tx7.setOnClickListener(this);
        TextView tx8 = view.findViewById(R.id.gps_btn);
        tx8.setOnClickListener(this);
//        RelativeLayout txs = view.findViewById(R.id.server_out);
//        txs.setOnClickListener(this);
//        RelativeLayout txc = view.findViewById(R.id.client_out);
//        txc.setOnClickListener(this);
        RelativeLayout txhead = view.findViewById(R.id.tba_menu_permutation_out);
        txhead.setOnClickListener(this);
        itemsService = new ItemsService();
        arrangeService = new ArrangeService();
        sendsService = new SendsService();
        deviceService = new DeviceService();
        this.initItemData(view);
        return view;
    }

    private void initItemData(View view){
        TextView item_info = view.findViewById(R.id.item_info);
        TextView item_info_name_val = view.findViewById(R.id.item_info_name_val);
        TextView item_info_ids_val = view.findViewById(R.id.item_info_ids_val);
        TextView item_info_client_val = view.findViewById(R.id.item_info_client_val);
        Items itmes = new Items();
        itmes.setStatus(1);//项目选中1 未选0
        List<Items> itmeslist = null;
        List<Device> devices = null;
        try {
            devices = deviceService.getDevice(new Device());
            deviceSta = devices.get(0).getStatus();
            itmeslist = itemsService.getItems(itmes);
            itmeslistLen = itmeslist.size();
            if(itmeslistLen>0){
                Items itmes1 = itmeslist.get(0);
                itmesMain = itmeslist.get(0);
                item_info.setText(itmes1.getName());
                item_info_name_val.setText(itmes1.getLocation());
                item_info_ids_val.setText(itmes1.getNum());
                item_info_client_val.setText(itmes1.getCustomerName());
            }else{
                item_info.setText("项目信息 >");
                item_info_name_val.setText("项目地点");
                item_info_ids_val.setText("项目编号");
                item_info_client_val.setText("项目名称");
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void arrangeCount(){
        Arrange arrange1 = new Arrange();
        arrange1.setItemId(itmesMain.getId());
        List<Arrange> arrangeLis = arrangeService.getArrange(arrange1);
        arrangeLen = arrangeLis.size();

    }
    private void sendsCount(){
        Sends sends = new Sends();
        sends.setItemId(itmesMain.getId());
        List<Sends> sendseLis =sendsService.getSends(sends);
        sendsLen = sendseLis.size();
    }

    @Override
    public void onResume() {//刷新数据
        super.onResume();

        initItemData(view);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.item_info_add://项目添加
                if(deviceSta==0){
                    Toast.makeText(view.getContext(), "请先绑定设备！", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    Intent intent = new Intent(this.getActivity(), ItemInfoActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.item_info://项目详情
                Intent intent1 = new Intent(this.getActivity(), ItemInfoListActivity.class);
                startActivity(intent1);
                break;
            case R.id.tba_menu_devicedata://数据采集记录
                if(deviceSta==0){
                    Toast.makeText(view.getContext(), "请先绑定设备！", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    if (itmeslistLen == 0) {
                        Toast.makeText(view.getContext(), "请先创建项目！", Toast.LENGTH_SHORT).show();
                        break;
                    } else {
                        arrangeCount();
                        if (arrangeLen == 0) {
                            Toast.makeText(view.getContext(), "请先添加排列设计！", Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                    Intent intent2 = new Intent(this.getActivity(), DeviceListActivity.class);
                    startActivity(intent2);
                }
                break;
            case R.id.tba_menu_inoutlist://发射接收记录
                if(deviceSta==0){
                    Toast.makeText(view.getContext(), "请先绑定设备！", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    if (itmeslistLen == 0) {
                        Toast.makeText(view.getContext(), "请先创建项目！", Toast.LENGTH_SHORT).show();
                        break;
                    } else {
                        arrangeCount();
                        if (arrangeLen == 0) {
                            Toast.makeText(view.getContext(), "请先添加排列设计！", Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                    Intent intent3 = new Intent(this.getActivity(), SendReceiveListActivity.class);
                    startActivity(intent3);
                }
                break;
            case R.id.tba_menu_inout://发射接收
                if(deviceSta==0){
                    Toast.makeText(view.getContext(), "请先绑定设备！", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    if (itmeslistLen == 0) {
                        Toast.makeText(view.getContext(), "请先创建项目！", Toast.LENGTH_SHORT).show();
                        break;
                    } else {
                        arrangeCount();
                        if (arrangeLen == 0) {
                            Toast.makeText(view.getContext(), "请先添加排列设计！", Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                    Intent intent4 = new Intent(this.getActivity(), SendReceiveAddActivity.class);
                    startActivity(intent4);
                }
                break;
            case R.id.tba_menu_permutation://排列设计
                if(deviceSta==0){
                    Toast.makeText(view.getContext(), "请先绑定设备！", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    if (itmeslistLen == 0) {
                        Toast.makeText(view.getContext(), "请先创建项目！", Toast.LENGTH_SHORT).show();
                        break;
                    }
                    Intent intent5 = new Intent(this.getActivity(), ArrangeAddActivity.class);
                    startActivity(intent5);
                }
                break;
            case R.id.tba_menu_datamining://数据采集
                if(deviceSta==0){
                    Toast.makeText(view.getContext(), "请先绑定设备！", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    if (itmeslistLen == 0) {
                        Toast.makeText(view.getContext(), "请先创建项目！", Toast.LENGTH_SHORT).show();
                        break;
                    } else {
                        arrangeCount();
                        if (arrangeLen == 0) {
                            Toast.makeText(view.getContext(), "请先添加排列设计！", Toast.LENGTH_SHORT).show();
                            break;
                        } else {
                            sendsCount();
                            if (sendsLen == 0) {
                                Toast.makeText(view.getContext(), "请先完成发射接收！", Toast.LENGTH_SHORT).show();
                                break;
                            }
                        }
                    }
                    Intent intent6 = new Intent(this.getActivity(), GatherDataActivity.class);
                    startActivity(intent6);
                }
                break;
            case R.id.alarm_btn://告警
                if(deviceSta==0){
                    Toast.makeText(view.getContext(), "请先绑定设备！", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    if (itmeslistLen == 0) {
                        Toast.makeText(view.getContext(), "请先创建项目！", Toast.LENGTH_SHORT).show();
                        break;
                    } else {
                        arrangeCount();
                        if (arrangeLen == 0) {
                            Toast.makeText(view.getContext(), "请先添加排列设计！", Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                    Intent intent7 = new Intent(this.getActivity(), AlarmListActivity.class);
                    startActivity(intent7);
                }
                break;
            case R.id.gps_btn://告警
//                if(itmeslistLen==0){
//                    Toast.makeText(view.getContext(), "请先创建项目！", Toast.LENGTH_SHORT).show();
//                    break;
//                }else{
//                    arrangeCount();
//                    if (arrangeLen == 0) {
//                        Toast.makeText(view.getContext(), "请先添加排列设计！", Toast.LENGTH_SHORT).show();
//                        break;
//                    }
//                }
                Intent intent8 = new Intent(this.getActivity(), GpsListActivity.class);
                startActivity(intent8);
                break;
//            case R.id.head_out://项目添加
//                Intent intents = new Intent(this.getActivity(), HeadActivity.class);
//                startActivity(intents);
//                break;
//            case R.id.server_out://项目添加
//                Intent intentser = new Intent(this.getActivity(), TcpServerActivity.class);
//                startActivity(intentser);
//                break;
//            case R.id.client_out://项目详情
//                Intent intentc = new Intent(this.getActivity(), TcpClientActivity.class);
//                startActivity(intentc);
//                break;
            default:

        }
    }
}
